﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_ScheduleDetailBusiness : BaseBusiness<PB_ScheduleDetail>, IPB_ScheduleDetailBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public PB_ScheduleDetailBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<PB_ScheduleDetail>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_ScheduleDetail>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_ScheduleDetail, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_ScheduleDetail> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_ScheduleDetail data)
        {
            data.Nums = 0;
            List<string> a = new List<string>();
            string sqlmain = @"SELECT *  FROM PB_ScheduleDetail   WHERE  UserID='" + data.UserId + "'  and    WorkDate  between   '" + Convert.ToDateTime(data.WorkDate).ToString("yyyy-MM-dd") + "'    and Type='" + data.Type + "' ";
            var atAllData = await Db.GetListBySqlAsync<PB_ScheduleDetail>(sqlmain);
            if (atAllData.Count > 0)
            {
                var q = Db.GetIQueryable<Base_User>().Where(x => x.Id == data.UserId).FirstOrDefault();
                a.Add(q.RealName);
            }

            if (a.Count > 0)
            {
                a = a.Distinct<string>().ToList();
                var result = String.Join(",", a.ToArray());
                throw new BusException("该时间段下以下用户已有排班:" + result);
            }
            await InsertAsync(data);
        }
        public async Task UpdateDataAsync(PB_ScheduleDetail data,string USERIDS)
        {

            var user =await Db.GetIQueryable<Base_User>().Where(x=>x.Id==data.UserId).FirstOrDefaultAsync();
            string msql = @"update PB_Dispatch set StaffId='"+data.UserId+ "', StaffName='"+user.RealName+ "',TeamType='"+data.Type+"'    where DispatchDate='" + Convert.ToDateTime(data.WorkDate).ToString("yyyy-MM-dd") + "' AND   StaffId='"+USERIDS+"' ";
            await ExecuteSqlAsync(msql);

            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
       
            await DeleteAsync(ids);
        }

        public  async Task<PageResult<PB_ScheduleDetailListDTO>> GetDataListShowAllNameAsync(PB_ProductionOrderDetailInput input)
        {
            Expression<Func<PB_ScheduleDetail, PB_Schedule,  Base_User, PB_ScheduleDetailListDTO>> select = (a, b,e) => new PB_ScheduleDetailListDTO
            {
           
                
                UserIdName=e.RealName,
            };
            var search = input.Search;
            select = select.BuildExtendSelectExpre();
            var q_ScheduleDetail = GetIQueryable().Where(w => w.ParentId == input.ParentId);
            var q = from a in q_ScheduleDetail.AsExpandable()
                    join b in Db.GetIQueryable<PB_Schedule>() on a.ParentId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
               
                    join e in Db.GetIQueryable<Base_User>() on a.UserId equals e.Id into ae
                    from e in ae.DefaultIfEmpty()


                    select @select.Invoke(a,b,e);
            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var keyword = $"%{search.Keyword}%";
                q = q.Where(x => EF.Functions.Like(search.Condition, keyword));
            }
           
            return await q.GetPageResultAsync(input);
        }

        public async Task<List<PB_ScheduleDetailDTO>> GetDataList()
        {
           
            Expression<Func<PB_ScheduleDetail, PB_ShiftType, Base_Department, Base_User,PB_Schedule, PB_ScheduleDetailDTO>> select = (a, b, c,d,e) => new PB_ScheduleDetailDTO
            {
            
                UserIdName = d.RealName,
                TypeName = b.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_ShiftType>() on a.Type equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on a.ParentId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                     join d in Db.GetIQueryable<Base_User>() on a.UserId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<PB_Schedule>() on a.ParentId equals e.Id into ae  
                    from e in ae.DefaultIfEmpty()
                
                    select @select.Invoke(a, b, c,d,e);
            q = q.OrderByDescending(x => x.WorkDate);
            var res = await q.ToListAsync();

            return res;
        }

        /// <summary>
        /// 批量修改 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public  async Task BatchtonightShift(PB_ScheduleDetail data)
        {
            List<string> props = GetFieldList(data);
            if (props.IsNullOrEmpty())
            {
                await UpdateAsync(data);
            }
            else
            {
                await UpdateAsync(data, props);
            }
        }

        /// <summary>
        /// 批量修改 生产排班将人员班别全部修成 白班
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task TestSqlDataAsync(string id)
        {
            if (!id.IsNullOrEmpty())
            {
                string[] arr = id.Split(',');
                string ids = "";
                foreach (var item in arr)
                {
                    ids += "'" + item + "',";
                }
                ids = ids.Substring(0, ids.Length - 1);
                await ExecuteSqlAsync("update PB_ScheduleDetail Set Type='1461677890910294016'  where UserId in (" + ids + ")");
            }
            else {
                throw new BusException("请求异常");
            }
            
        }
        /// <summary>
        /// 批量修改 将排班人员 统一修改成 夜班
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task BatchNightShift(string id)
        {
            if (!id.IsNullOrEmpty())
            {
                string[] arr = id.Split(',');
                string ids = "";
                foreach (var item in arr)
                {
                    ids += "'" + item + "',";
                }
                ids = ids.Substring(0, ids.Length - 1);   //1461677890910294017 夜班
                await ExecuteSqlAsync("update PB_ScheduleDetail Set Type='1461677890910294017'  where UserId in (" + ids + ")");
            }
            else {
                throw new Exception("请求异常");
            }
        }
        /// <summary>
        /// 根据创建时间 和结束时间进行查询
        /// </summary>
        /// <param name="CreateTime"></param>
        /// <param name="oldTime"></param>
        /// <returns></returns>
        public async Task<List<PB_ScheduleDetailDTO>> GetDataTimeList(DateTime? CreateTime, DateTime? oldTime)
        {
            Expression<Func<PB_ScheduleDetail, PB_ShiftType, Base_Department, Base_User, PB_Schedule, PB_ScheduleDetailDTO>> select = (a, b, c, d, e) => new PB_ScheduleDetailDTO
            {
                newTime=e.WorkingDate,
                oldtime=e.shiftarrangementDate,
                UserIdName = d.RealName,
                TypeName = b.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_ShiftType>() on a.Type equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on a.ParentId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<Base_User>() on a.UserId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<PB_Schedule>() on a.ParentId equals e.Id into ae
                    from e in ae.DefaultIfEmpty()
                    select @select.Invoke(a, b, c, d, e);
           q= q.Where(m => m.newTime == CreateTime && m.oldtime == oldTime);
            var res = await q.ToListAsync();

            return res;
        }
        /// <summary>
        /// 根据组织编号进行查询人员信息
        /// </summary>
        /// <param name="Deptid"></param>
        /// <returns></returns>
        public  async Task<List<PB_ScheduleDetailDTO>> GetDataDeptList(string Deptid)
        {
            Expression<Func<PB_ScheduleDetail, PB_ShiftType, Base_Department, Base_User, PB_Schedule, PB_ScheduleDetailDTO>> select = (a, b, c, d, e) => new PB_ScheduleDetailDTO
            {
              
               Deptid=e.WorkShopID,
                UserIdName = d.RealName,
                TypeName = b.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_ShiftType>() on a.Type equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on a.ParentId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<Base_User>() on a.UserId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<PB_Schedule>() on a.ParentId equals e.Id into ae
                    from e in ae.DefaultIfEmpty()
                    select @select.Invoke(a, b, c, d, e);
           q= q.Where(m => m.Deptid == Deptid);
            var res = await q.ToListAsync();

            return res;
        }
        /// <summary>
        /// 查找白班的用户信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<PB_ScheduleDetailDTO>> GetDataByDayshift()
        {
            Expression<Func<PB_ScheduleDetail, PB_ShiftType, Base_Department, Base_User, PB_Schedule, PB_ScheduleDetailDTO>> select = (a, b, c, d, e) => new PB_ScheduleDetailDTO
            {

                Deptid = e.WorkShopID,
                UserIdName = d.RealName,
                TypeName = b.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_ShiftType>() on a.Type equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on a.ParentId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<Base_User>() on a.UserId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<PB_Schedule>() on a.ParentId equals e.Id into ae
                    from e in ae.DefaultIfEmpty()
                    select @select.Invoke(a, b, c, d, e);
            q = q.Where(m => m.Type == "1461677890910294016");
            var res = await q.ToListAsync();

            return res;
        }
        /// <summary>
        /// 查找夜班的用户信息
        /// </summary>
        /// <returns></returns>
        public  async Task<List<PB_ScheduleDetailDTO>> GetDataByNightshift()
        {
            Expression<Func<PB_ScheduleDetail, PB_ShiftType, Base_Department, Base_User, PB_Schedule, PB_ScheduleDetailDTO>> select = (a, b, c, d, e) => new PB_ScheduleDetailDTO
            {

                Deptid = e.WorkShopID,
                UserIdName = d.RealName,
                TypeName = b.Name,
                ParentIdName=c.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_ShiftType>() on a.Type equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on a.ParentId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<Base_User>() on a.UserId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<PB_Schedule>() on a.ParentId equals e.Id into ae
                    from e in ae.DefaultIfEmpty()
                    select @select.Invoke(a, b, c, d, e);
            q = q.Where(m => m.Type == "1461677890910294017");
            var res = await q.ToListAsync();

            return res;
        }

        /// <summary>
        /// 查询 所有排班人员信息 并且根据开始时间和结束时间进行查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<PB_ScheduleDetailDTO>> GetDataListAsyncDtoList(PageInput<PB_ScheduleDetailConditionDTO> input)
        {
            Expression<Func<PB_ScheduleDetail, PB_ShiftType,  Base_User, PB_Schedule, Base_Department, PB_ScheduleDetailDTO>> select = (a, b,  d, e ,f) => new PB_ScheduleDetailDTO
            {
                UserIdName = d.RealName,
                WorkName=f.Name,
                Workid=f.Id,
                TypeName = b.Name,
                Shangban = b.Morningshifttime,
                Xiaban = b.Nightshifttime,
                UserCode=d.UserCode,
                State=e.Toexamine,
                DeptId=e.WorkShopID,
            };
            var search = input.Search;
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_ShiftType>() on a.Type equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join d in Db.GetIQueryable<Base_User>() on a.UserId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<PB_Schedule>() on a.ParentId equals e.Id into ae
                    from e in ae.DefaultIfEmpty()
                    join f in Db.GetIQueryable<Base_Department>() on e.WorkShopID equals f.Id into ef
                    from f in ef.DefaultIfEmpty()
                    select @select.Invoke(a, b, d, e,f);
               if (!search.StartDate.IsNullOrEmpty() && !search.EndDate.IsNullOrEmpty())
               {
                DateTime StartDate = Convert.ToDateTime(Convert.ToDateTime(search.StartDate).Date.ToString());
                DateTime EndDate = Convert.ToDateTime(Convert.ToDateTime(search.EndDate).Date.ToString()).AddDays(1).AddSeconds(-1);
                q = q.Where(x => StartDate <= x.WorkDate && x.WorkDate <= EndDate);
               }

            if (!search.Name.IsNullOrEmpty()) {
                q = q.Where(x=> x.UserIdName==search.Name||x.UserCode.Contains(search.Name));
            }
            if (!search.WorkName.IsNullOrEmpty())
            {
                q = q.Where(x => x.WorkName .Contains(search.WorkName));
            }

            q = q.Where(m => _operator.Property.DeptIds.Contains(m.Workid));
            q = q.Where(m => m.State == 2);

            var AA = await q.GetPageResultAsync(input);
            foreach (var itmess in AA.Data) {
                string sqlMain = @"SELECT D.* FROM PB_ScheduleDetail  AS A
inner JOIN  PB_Dispatch AS B on A.UserId=B.StaffId and A.WorkDate=B.DispatchDate
INNER JOIN  PB_DispatchDetail AS C ON C.ParentId=B.Id
INNER JOIN PB_ProductionOrder  AS D ON C.OrderId=D.Id
WHERE A.UserId='"+itmess.UserId+"' and A.WorkDate='2022-03-22'   GROUP BY D.Id ";
                itmess.ProductionOrdeNameList = await Db.GetListBySqlAsync<PB_ProductionOrderlistdtos>(sqlMain); ;
            }
            return AA;
        }

        public async Task<List<PB_ScheduleDetailUserDTO>> GetDataListid(string parid)
        {
            Expression<Func<PB_ScheduleDetail, Base_User, Base_Department, PB_ShiftType, PB_Schedule, Base_Department, PB_ScheduleDetailUserDTO>> select = (a, b, c, d,e,f) => new PB_ScheduleDetailUserDTO
            {
                RealName = b.RealName,
                DepartmentId = c.Id,
                DepartmentName = c.Name,
                TypeName = d.Name,
                WorkName=f.Name,
              UserCode=b.UserCode,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in Db.GetIQueryable<PB_ScheduleDetail>().AsExpandable()
                    join b in Db.GetIQueryable<Base_User>() on a.UserId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on b.DepartmentId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<PB_ShiftType>() on a.Type equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<PB_Schedule>() on a.ParentId equals e.Id into ae
                    from e in ae.DefaultIfEmpty()
                    join f in Db.GetIQueryable<Base_Department>() on e.WorkShopID equals f.Id into ef
                    from f in ef.DefaultIfEmpty()
                    select @select.Invoke(a, b, c, d,e,f);
            q = q.OrderByDescending(x => x.WorkDate);
            q = q.Where(m => m.ParentId ==parid);
            var res = await q.ToListAsync();

            return res;
        }
      
        public  async Task<List<PB_ScheduleDetailUserDTO>> GetDataListids(string ids)
        {
            Expression<Func<PB_ScheduleDetail, Base_User, Base_Department, PB_ShiftType, PB_ScheduleDetailUserDTO>> select = (a, b, c, d) => new PB_ScheduleDetailUserDTO
            {
                RealName = b.RealName,
                DepartmentId = c.Id,
                DepartmentName = c.Name,
                TypeName = d.Name,
                HeadImg=b.HeadImg,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in Db.GetIQueryable<PB_ScheduleDetail>().AsExpandable()
                    join b in Db.GetIQueryable<Base_User>() on a.UserId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on b.DepartmentId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<PB_ShiftType>() on a.Type equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    select @select.Invoke(a, b, c, d);
            q = q.OrderByDescending(x => x.WorkDate);
            q = q.Where(m => m.Id == ids);
            var res = await q.ToListAsync();

            return res;
        }

        public  async Task<PageResult<PB_ScheduleDetailDTO>> GetDataListAsyncDtoListParids(PageInput<PB_ScheduleDetailConditionDTO> input, string parids)
        {
            Expression<Func<PB_ScheduleDetail, PB_ShiftType, Base_User, PB_Schedule, Base_Department, PB_ScheduleDetailDTO>> select = (a, b, d, e, f) => new PB_ScheduleDetailDTO
            {
                UserIdName = d.RealName,
                WorkName = f.Name,
                Workid = f.Id,
                TypeName = b.Name,
                Shangban = b.Morningshifttime,
                Xiaban = b.Nightshifttime,
                
            };
            var search = input.Search;
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_ShiftType>() on a.Type equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join d in Db.GetIQueryable<Base_User>() on a.UserId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<PB_Schedule>() on a.ParentId equals e.Id into ae
                    from e in ae.DefaultIfEmpty()
                    join f in Db.GetIQueryable<Base_Department>() on e.WorkShopID equals f.Id into ef
                    from f in ef.DefaultIfEmpty()
                    select @select.Invoke(a, b, d, e, f);
            if (!search.StartDate.IsNullOrEmpty() && !search.EndDate.IsNullOrEmpty())
            {
                DateTime StartDate = Convert.ToDateTime(Convert.ToDateTime(search.StartDate).Date.ToString());
                DateTime EndDate = Convert.ToDateTime(Convert.ToDateTime(search.EndDate).Date.ToString()).AddDays(1).AddSeconds(-1);
                q = q.Where(x => StartDate <= x.WorkDate && x.WorkDate <= EndDate);
            }

            if (!search.Name.IsNullOrEmpty())
            {
                q = q.Where(x => x.UserIdName == search.Name);
            }
            q = q.OrderByDescending(x => x.WorkDate);//排序
            q = q.Where(p=>p.ParentId==parids);
            return await q.GetPageResultAsync(input);
        }

        public  async Task<PageResult<PB_ScheduleDetailUserDTO>> GetDataListByAreaids(PageInput<ConditionDTO> input, string ID)
        {
            var where = LinqHelper.True<PB_ScheduleDetailUserDTO>();
            var search = input.Search;
            Expression<Func<PB_ScheduleDetail, Base_User, Base_Department, PB_ShiftType, PB_Schedule, Base_Department, PB_ScheduleDetailUserDTO>> select = (a, b, c, d, e, f) => new PB_ScheduleDetailUserDTO
            {
                UserCode=b.UserCode,
                RealName = b.RealName,
                DepartmentId = c.Id,
                DepartmentName = c.Name,
                TypeName = d.Name,
                WorkName = f.Name,

            };
            select = select.BuildExtendSelectExpre();
            var q = from a in Db.GetIQueryable<PB_ScheduleDetail>().AsExpandable()
                    join b in Db.GetIQueryable<Base_User>() on a.UserId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on b.DepartmentId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<PB_ShiftType>() on a.Type equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<PB_Schedule>() on a.ParentId equals e.Id into ae
                    from e in ae.DefaultIfEmpty()
                    join f in Db.GetIQueryable<Base_Department>() on e.WorkShopID equals f.Id into ef
                    from f in ef.DefaultIfEmpty()
                    select @select.Invoke(a, b, c, d, e, f);
            where = where.And(m=>m.ParentId==ID);
            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_ScheduleDetailUserDTO, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }
            return await q.Where(where).GetPageResultAsync(input);
        }
        /// <summary>
        /// 大屏显示当日派工    出勤报工人数
        /// </summary>
        /// <param name="Day">前第几天</param>
        /// <returns>当日派工     出勤报工人数</returns>
        public ScreenDisplayValue3DTO GetTodayScheduleAttendNums(int Day)
        {           
            string strSQL = @"SELECT COUNT(DISTINCT StaffId) AS value
FROM PB_Dispatch
WHERE TO_DAYS(CURDATE())-TO_DAYS(DispatchDate)=" + Day.ToString();
            var res = Db.GetListBySqlAsync<ScreenDisplayValue2DTO>(strSQL).Result.FirstOrDefault();
            if (res != null)
            {
                return new Entity.DTO.ScreenDisplayValue3DTO { value = res.value.ToString() + "人" };
            }
            return new Entity.DTO.ScreenDisplayValue3DTO { value = "0" + "人" };
        }
        /// <summary>
        /// 大屏显示七日派工人数  出勤人数
        /// </summary>
        /// <param name="Day">前第几天</param>
        /// <returns>七日派工人数  出勤人数</returns>
        public ScreenDisplayBarChartDTO Get7DaysScheduleAttendNums(int Day)
        {
            ScreenDisplayBarChartDTO res = new ScreenDisplayBarChartDTO();
            res.categories = new List<string>();
            res.name = "派工数";
            string strSQL;
            for (int i = 6;i>-1; i--)
            {
                strSQL = @"SELECT COUNT(DISTINCT StaffId) AS value
FROM PB_Dispatch
WHERE TO_DAYS(CURDATE())-TO_DAYS(DispatchDate)=" + (i + Day).ToString();
                var temp = Db.GetListBySql<ScreenDisplayValueDTO>(strSQL).FirstOrDefault();
                res.series[0].data.Add(temp.value);             
                string strJobDate = DateTime.Now.AddDays(-1 * (i + Day)).ToString("MM.dd");
                res.categories.Add(strJobDate);
            }
            return res;
        }

        /// <summary>
        /// 大屏显示当日出勤率
        /// </summary>
        /// <param name="Day"></param>
        /// <returns></returns>
        public ScreenDisplayDashboardDTO GetScheduleAttendRate(int Day)
        {          
            string strSQL = @"SELECT COUNT(DISTINCT StaffId) AS value
FROM PB_Dispatch
WHERE TO_DAYS(CURDATE())-TO_DAYS(DispatchDate)=" + Day.ToString();
            int dispatchNums = Db.GetListBySqlAsync<ScreenDisplayValue2DTO>(strSQL).Result.FirstOrDefault().value;
            strSQL = @"SELECT COUNT(DISTINCT UserId) AS value
FROM PB_ScheduleDetail WHERE TO_DAYS(CURDATE())-TO_DAYS(WorkDate)=" + Day.ToString();
            int scheduleNums = Db.GetListBySqlAsync<ScreenDisplayValue2DTO>(strSQL).Result.FirstOrDefault().value;
            ScreenDisplayDashboardDTO res = new ScreenDisplayDashboardDTO();
            if (scheduleNums != 0)
            {
                if (dispatchNums >= scheduleNums)
                {
                    res.value = 100;
                }
                else
                {
                    res.value = (decimal)dispatchNums / (decimal)scheduleNums * 100;
                }
                res.value = Convert.ToDecimal(res.value.ToString("#0.##"));
            }
            else
            {
                res.value = 0;
            }
            return res;
        }
        #endregion

        #region 私有成员

        #endregion
    }
}